package com.blakequ.androidblemanager.utils;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * Created by xiachao on 2017/8/10.
 */

public class JsonUtil {

    /**
     * [将字符串转为json]<BR>
     *
     * @param
     */
    public static <T> T toObject(String json, Class<T> clazz) {
        T object = null;
        String errorString = null;
        try {
            object = JSONObject.parseObject(json, clazz);
        } catch (Exception e) {
            errorString = TextUtils.isEmpty(json) ? "" : e != null ? e.toString() + json:"";//e != null ? e.toString() : "";
        }
        if (!TextUtils.isEmpty(errorString)) {
//            show("接口解析失败:\n" + errorString);
            Log.d("AppUtils-Json", "JsonResponse Exception:" + errorString);
//            CrashUtils.getInstance().netSubmitLog(AiyigeExceptionModel.type_json_parsing, "Json解析异常：\n" + errorString, AiyigeExceptionModel.rating_3);
//            ExceptionUtils.uploadJsonToObjectException("json转换异常,"+"类名："+clazz.getSimpleName()+" 错误详情："+errorString);
        }
        return object;
    }


    public static <T> List<T> toObjectList(String json, Class<T> clazz) {
        List<T> list = null;
        String errorString = null;
        try {
            list = JSON.parseArray(json, clazz);
        } catch (Exception e) {
            errorString = TextUtils.isEmpty(json) ? "" : e != null ? e.toString() + json:"";//e != null ? e.toString() : "";
//            errorString = TextUtils.isEmpty(json) ? "" : e.toString()+json;//e != null ? e.toString() : "";
        }
        if (!TextUtils.isEmpty(errorString)) {
//            show("接口解析失败:\n" + errorString);
            Log.d("AppUtils-Json", "JsonResponse Exception:" + errorString);
//            CrashUtils.getInstance().netSubmitLog(AiyigeExceptionModel.type_json_parsing, "Json解析异常：\n" + errorString, AiyigeExceptionModel.rating_3);
        }
        return list;
    }


    /**
     * [class转为json字符串]<BR>
     *
     * @param
     */
    public static String toString(Object object) {
        String error = "";
        try {
            error = JSONObject.toJSONString(object);
        } catch (Exception e) {
            Log.i("","");
        }
        return error;
    }

}
